package com.abkj.platform.util;

import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;

public class SerialPortUtil
{
  public static int getDecLen(float val)
  {
    int n = 0;
    while (val - (int)val > 0.0F) {
      val *= 10.0F;
      n++;
    }
    return n;
  }

  public static byte[] getTwoByte(float val)
  {
    byte[] buf = new byte[2];
    int n = getDecLen(val);
    float rval = Math.abs(val) * (float)Math.pow(10.0D, n);
    short s;
    if (val < 0.0F)
      s = (short)(n << 13 | (short)(int)rval | 0x8000);
    else {
      s = (short)(n << 13 | (short)(int)rval);
    }
    buf[0] = ((byte)(s >>> 8 & 0xFF));
    buf[1] = ((byte)(s & 0xFF));
    return buf;
  }

  public static void main(String[] args) {
    byte[] ret = getTwoByte(20.1F);
    System.out.println(Integer.toHexString(ret[0]));
    System.out.println(Integer.toHexString(ret[1]));

    System.out.println(0);

    System.out.println(-1);
  }

  public static boolean crcValidate(byte[] cmd)
  {
    int len = cmd.length;
    byte[] crc = PublicTool.intToBytes(CRC8.r_crc16_by_bit(cmd, len - 2));
    if ((crc[3] == cmd[(len - 2)]) && (crc[2] == cmd[(len - 1)]))
      return true;
    return false;
  }

  public static boolean crcPersonValidate(byte[] cmd)
  {
    int len = cmd.length;
    byte[] crc = PublicTool.intToBytes(CRC8.r_crc16_by_bit(cmd, len - 4));
    if ((crc[3] == cmd[(len - 1)]) && (crc[2] == cmd[(len - 2)]))
      return true;
    return false;
  }

  private static byte[] _write(SerialPort sp, byte[] cmd, int size, int interval) {
    InputStream in = null;
    byte[] recv = new byte[size];
    try {
      in = write(sp, cmd);
      if (timeOut(in, size, interval))
        return null;
      int n = 0;
      boolean first = false;
      while ((n = in.read()) != -1)
        if (n == 237) {
          first = true;
          break;
        }
      byte[] arrayOfByte1;
      if (first) {
        if (timeOut(in, size - 1, interval))
          return null;
        recv[0] = -19;
        in.read(recv, 1, size - 1);
        return recv;
      }
      return recv;
    }
    catch (IOException e) {
      e.printStackTrace();
      return null;
    } catch (InterruptedException e) {
      e.printStackTrace();
      return null;
    } finally {
      try {
        if (in != null)
          in.close();
      }
      catch (IOException localIOException7)
      {
      }
    }
  }

  public static byte[] write(SerialPort sp, byte[] cmd, int size)
  {
    return _write(sp, cmd, size, 1000);
  }

  public static byte[] write(SerialPort sp, byte[] cmd, int size, int interval)
  {
    return _write(sp, cmd, size, interval);
  }

  public static byte[] readStationInfo(SerialPort sp, byte[] cmd, int interval)
  {
    InputStream in = null;
    try {
      in = write(sp, cmd);
      if (timeOut(in, 6, interval))
        return null;
      int n = 0;
      boolean first = false;
      while ((n = in.read()) != -1) {
        if (n == 237) {
          first = true;
          break;
        }
      }
      if (first) {
        byte[] prev = new byte[6];
        prev[0] = -19;
        if (timeOut(in, 5, interval))
          return null;
        in.read(prev, 1, 5);
        byte[] arrayOfByte1;
        if (prev[3] == 0) {
          return prev;
        }
        if (timeOut(in, 35, interval))
          return null;
        byte[] next = new byte[35];
        in.read(next);
        byte[] ret = new byte[41];
        System.arraycopy(prev, 0, ret, 0, 6);
        System.arraycopy(next, 0, ret, 6, 35);
        return ret;
      }

      return null;
    }
    catch (IOException e) {
      e.printStackTrace();
      return null;
    } catch (InterruptedException e) {
      e.printStackTrace();
      return null;
    } finally {
      try {
        if (in != null)
          in.close();
      }
      catch (IOException localIOException9)
      {
      }
    }
  }

  public static byte[] writeCmd(SerialPort sp, byte[] cmd)
  {
    InputStream in = null;
    try {
      in = write(sp, cmd);
      Thread.sleep(1000L);
      byte[] head = getPack(in, 3);
      if (head == null) {
        return null;
      }
      int length = head[2];

      byte[] data = getPack(in, length + 2);

      ByteBuffer buffer = ByteBuffer.allocate(head.length + data.length);
      buffer.put(head);
      buffer.put(data);
      buffer.flip();
      return buffer.array();
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (in != null)
          in.close();
      } catch (IOException localIOException6) {
      }
    }
    return null;
  }

  private static byte[] getPack(InputStream in, int len)
    throws Exception
  {
    if (in.available() == 0) {
      System.out.println("Get pack fail.");
      return null;
    }
    System.out.println("get pack===============");
    int newData = 0;

    byte[] msgPack = new byte[len];
    System.out.println("for loop begin");
    for (int i = 0; i < len; i++) {
      if ((newData = in.read()) != -1) {
        msgPack[i] = ((byte)newData);
        System.out.println("data[" + i + "]==>" + (byte)newData);
      }
    }
    System.out.println("for loop end");
    System.out.println("Get pack success.");
    return msgPack;
  }

  public byte[] getData(InputStream in)
    throws Exception
  {
    Thread.sleep(3000L);
    byte[] head = getPack(in, 3);
    if (head == null) {
      return null;
    }
    int length = head[2];

    byte[] data = getPack(in, length + 2);
    ByteBuffer buffer = ByteBuffer.allocate(head.length + data.length);
    buffer.put(head);
    buffer.put(data);
    buffer.flip();
    return buffer.array();
  }

  public static InputStream write(SerialPort sp, byte[] cmd)
  {
    if (sp == null) {
      System.out.println("open serial port error.");
      return null;
    }
    OutputStream out = null;
    try {
      out = sp.getOutputStream();
      out.write(cmd);
      out.flush();
      out.close();
      return sp.getInputStream();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (out != null)
          out.close();
      } catch (IOException localIOException3) {
      }
    }
    return null;
  }

  public static boolean timeOut(InputStream in, int size, int interval)
    throws InterruptedException, IOException
  {
    int count = in.available(); int i = 0;
    while (count < size) {
      count = in.available();
      Thread.sleep(10L);
      if (i++ >= interval / 10)
      {
        System.out.println("read time out.");
        return true;
      }
    }
    return false;
  }
}